package com.gw.vip.server.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gw.vip.server.po.TmpOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * @author GW
 * @description
 * @since 2022-05-07
 */
public interface TmpOrderMapper extends BaseMapper<TmpOrder> {
    /**
     * 清空表
     */
    @Update("TRUNCATE TABLE tmp_order")
    void truncate();

    /**
     * 初始化数据
     * @param clearDate
     * @return
     */
    @Insert("INSERT INTO tmp_order SELECT * FROM bs_order WHERE clear_date=#{clearDate}")
    Integer initData(LocalDate clearDate);

    /**
     * 从历史加载清算数据
     * @param clearDate
     */
    @Insert("INSERT INTO tmp_order SELECT * FROM his_order WHERE clear_date=#{clearDate}")
    void initDataFromHis(LocalDate clearDate);

    /**
     * 汇总交易
     * @param clearDate
     * @return
     */
    @Select("SELECT SUM(txn_amt*balance_dir) sum_amt FROM tmp_order LEFT JOIN sys_order_type ON tmp_order.order_type = sys_order_type.id WHERE status='S' AND tmp_order.clear_date=#{clearDate}")
    BigDecimal orderSum(LocalDate clearDate);

    /**
     * 根据清算日期查询
     * @param clearDate
     * @return
     */
    @Select("SELECT * FROM tmp_order WHERE clear_date=#{clearDate}")
    List<TmpOrder> listByClearDate(LocalDate clearDate);
}
